package com.witmore.skyline.web.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Description: QuerySortUtil
 * Author: 豆子高
 * Date: 2025/3/6 12:20
 */
public class QuerySortUtil {
    public static <T> void addSort(LambdaQueryWrapper<T> wrapper, String sortField, String sortOrder,
                                   Map<String, SFunction<T, ?>> sortableFields) {
        if (StringUtils.isBlank(sortField)) {
            return;
        }
        SFunction<T, ?> tFunction = sortableFields.get(sortField);
        if (tFunction != null) {
            if (StringUtils.isEmpty(sortOrder) || "asc".equalsIgnoreCase(sortOrder)) {
                wrapper.orderByAsc(tFunction);
            } else if ("desc".equalsIgnoreCase(sortOrder)) {
                wrapper.orderByDesc(tFunction);
            }
        }
    }
}
